package bz.ziro.kanbe.controller.mng.browser;

import java.util.Date;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import bz.ziro.kanbe.dao.TemplateBrowserDao;
import bz.ziro.kanbe.logic.TemplateLogic;
import bz.ziro.kanbe.model.TemplateBrowser;

/**
 * ブラウザデータの更新
 * @author Administrator
 */
public class UpdateController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(UpdateController.class.getName());

    @Override
    public Navigation run() {

        //サイトのキーを取得
        String siteKeyBuf = sessionScope("siteKey");
        String templateKeyBuf = param("tempKey");
        String keyBuf = param("browserKey");
        
        //ブラウザデータを取得
        TemplateBrowser browser = TemplateBrowserDao.find(Long.valueOf(siteKeyBuf),Long.valueOf(templateKeyBuf), Long.valueOf(keyBuf));

        BeanUtil.copy(request, browser);
       
        //テンプレートをコンパイル
        String rtn = TemplateLogic.compile(browser.getTemplateData());
        if ( rtn != null ) {
        	requestScope("message",rtn);
        	return forward("message.jsp");
        }

        //作成者情報を登録
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        browser.setEditDate(new Date());
        browser.setEditor(user);

        Datastore.put(browser);
        return forward("../message.jsp");
    }
}
